﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace kompot_wtf
{
    public class GameObject
    {
        public static int ScreenWidth, ScreenHeight;
        public static Vector2 PlayerPosition;

    	protected MobAnimation Animation { get; set; }
        protected float AnimationUpdateInterval { get; set; }
        protected float TimeFromLastAnimationUpdate { get; set; }

        protected Vector2 MoveDirection { get; set; }
        protected Vector2 LookDirection { get; set; }

		public float Radius;
        public static Vector2 TransformWorldPosToScreenPos(Vector2 worldPos)
        {
            return new Vector2(ScreenWidth / 2, ScreenHeight / 2) + PlayerPosition - worldPos;
        }

        public Vector2 Position;

        protected Texture2D Texture { get; set; }

        public void Load(string texFilename)
        {
			Texture = WTF.ContentMgr.Load<Texture2D>(texFilename);
            Radius = 30.0f;
        }

        public virtual void Update(float dt)
        {

        }

        protected void UpdateAnimationFrame(float dt)
        {      
            TimeFromLastAnimationUpdate += dt;
            if (TimeFromLastAnimationUpdate >= AnimationUpdateInterval)
            {
                TimeFromLastAnimationUpdate = 0.0f;
                Animation.NextFrame();
            }
        }

        protected void UpdateAnimationDirection()
        {
            var x = LookDirection.X;
            var y = LookDirection.Y;

            var absX = Math.Abs(x);
            var absY = Math.Abs(y);

            var absDiff = Math.Abs(absX - absY);
            var absMax = Math.Max(absX, absY);

            if (y < 0 && absX < absY / 2)
            {
                Animation.ChangeDirection(MobAnimationDirection.Top);
            }
            else if (y < 0 && x > 0 && absDiff <= absMax / 2)
            {
                Animation.ChangeDirection(MobAnimationDirection.TopRight);
            }
            else if (x > 0 && absY < absX / 2)
            {
                Animation.ChangeDirection(MobAnimationDirection.Right);
            }
            else if (x > 0 && y > 0 && absDiff <= absMax / 2)
            {
                Animation.ChangeDirection(MobAnimationDirection.BottomRight);
            }
            else if (y > 0 && absX < absY / 2)
            {
                Animation.ChangeDirection(MobAnimationDirection.Bottom);
            }
            else if (y > 0 && x < 0 && absDiff <= absMax / 2)
            {
                Animation.ChangeDirection(MobAnimationDirection.BottomLeft);
            }
            else if (x < 0 && absY < absX / 2)
            {
                Animation.ChangeDirection(MobAnimationDirection.Left);
            }
            else if (x < 0 && y < 9 && absDiff <= absMax / 2)
            {
                Animation.ChangeDirection(MobAnimationDirection.TopLeft);
            }
        }
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            var transformedPosition = TransformWorldPosToScreenPos(Position);

            if (Animation != null)
            {
                Animation.Draw(spriteBatch, transformedPosition);
            }
            else
            {
                spriteBatch.Begin();
                spriteBatch.Draw(Texture, transformedPosition - new Vector2(Texture.Width / 2, Texture.Height / 2), Color.White);
                spriteBatch.End();
            }
        }
        public static bool CheckCollision(GameObject go1, GameObject go2)
        {
            return Vector2.Distance(go1.Position, go2.Position) < go1.Radius + go2.Radius;
        }
    }
}
